package com.yonyou.brigade.third_parties.third.service.impl;

import com.yonyou.brigade.third_parties.third.service.IAuthUserUpdateObserver;
import com.yonyou.brigade.third_parties.third.service.IAuthUserUpdateObserverable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AuthUserUpdateObserverable  implements IAuthUserUpdateObserverable {

    //注意到这个List集合的泛型参数为Observer接口，设计原则：面向接口编程而不是面向实现编程
    private List<IAuthUserUpdateObserver> list;
    private String message;

    public AuthUserUpdateObserverable() {
        list = new ArrayList<IAuthUserUpdateObserver>();
    }


    public void registerObserver(IAuthUserUpdateObserver o) {

        list.add(o);
    }

    public void removeObserver(IAuthUserUpdateObserver o) {
        if(!list.isEmpty())
            list.remove(o);
    }

    //遍历
    public Map notifyObserver(List<String> userids, String orgpk, String authUserid) {
        Map result=new HashMap();
        for(int i = 0; i < list.size(); i++) {
            IAuthUserUpdateObserver oserver = list.get(i);
            Map tempResult = oserver.updateAuthUser(userids, orgpk,authUserid);
            if(!tempResult.get("code").equals("0"))
            {
                result=tempResult;
                return  result;
            }
        }
        result.put("code","0");
        result.put("information","操作成功");
        return  result;
    }

}

